package cn.harry.controller.wx;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.CommonPage;
import cn.harry.common.api.CommonResult;
import cn.harry.common.enums.BusinessType;
import cn.harry.common.utils.SysUserUtils;
import cn.harry.sys.entity.SysUser;
import cn.harry.wx.entity.WxAccount;
import cn.harry.wx.service.WxAccountService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 公众号账号
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Api(tags = "wx-account => 公众号账号")
@RestController
@RequestMapping("wx/account")
public class WxAccountController {
    @Resource
    private WxAccountService wxAccountService;

    @ApiOperation("listByUserId => 根据用户ID获取公众号账号列表")
    @PreAuthorize("@ss.hasPermi('wx:account:list')")
    @GetMapping(value = "/listByUserId")
    public CommonResult<List<WxAccount>> listByUserId() {
        SysUser user = SysUserUtils.getSysUser();
        List<WxAccount> list = wxAccountService.getListByUserId(user.getId());
        return CommonResult.success(list);
    }


    @ApiOperation("list => 分页获取公众号账号列表")
    @PreAuthorize("@ss.hasPermi('wx:account:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<WxAccount>> list(WxAccount wxAccount,
                                                    @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<WxAccount> page = wxAccountService.getPage(wxAccount, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 查询公众号账号详细
     */
    @ApiOperation("{id} => 获取公众号账号信息")
    @PreAuthorize("@ss.hasPermi('wx:account:query')")
    @GetMapping(value = "/{id}")
    public CommonResult<WxAccount> getInfo(@PathVariable Long id) {
        return CommonResult.success(wxAccountService.selectById(id));
    }

    @ApiOperation("create => 新建公众号账号")
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody WxAccount wxAccount) {
        int count = wxAccountService.create(wxAccount);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("update/{id} => 修改指定公众号账号")
    @PreAuthorize("@ss.hasPermi('wx:account:edit')")
    @SysLog(title = "公众号账号数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id, @RequestBody WxAccount wxAccount) {
        int count = wxAccountService.update(id, wxAccount);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("deleteByIds/{ids} => 删除指定公众号账号")
    @PreAuthorize("@ss.hasPermi('wx:account:remove')")
    @SysLog(title = "公众号账号类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<Integer> deleteByIds(@PathVariable Long[] ids) {
        int count = wxAccountService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
